

function FileHandler(){
	
	FileHandler.shapeExtension = ".shp";
	FileHandler.drawingExtension = ".dhp";
	
	// Contains imported shapes
	this.importedShapes = [];
	
	// Exports one shape
	this.exportShape = function(shape){
		
		// Setting up the beginning of the file !
		
		// File size used for allocating a correct dataView size
		var fileSize = 0;
		
		// Current byte reading
		var byteoffset = 0;
		
		fileSize = fileSize + 2; // 2 bytes
		
		// Gets binary size of the shape
		fileSize = fileSize + shape.getBinarySize();
		
		var buffer = new ArrayBuffer(fileSize); 
		
		var dataView = new DataView(buffer);
		
		// Filling the dataView
		byteoffset = shape.generateHexString(dataView, byteoffset);
		
		// Creating the blob
		
		var saveBlob = new Blob([dataView], {type: 'application/octet-binary'});
		
		// Saving
		saveAs(saveBlob, document.getElementById("exportFileName").value + FileHandler.shapeExtension);
	}
	
	// Imports a shape
	this.importShape = function(){
		
		
		var dataBuffer = null;
		
		var files = document.getElementById('filesToImport').files;
		
		if (!files.length) {
		  alert('Please select a file!');
		  return;
		}

		var file = files[0];
		
		
		
		var start = 0;
		
		var stop = file.size - 1;

		var reader = new FileReader();
		
		var blob = file.slice(start, stop + 1);
		
		var globalThis = this;
		
		reader.readAsArrayBuffer(blob);
		
		reader.onloadend = function(evt) {
		
			dataBuffer = new DataView(evt.target.result);
			
			
			var byteoffset = 0;
			
			var tmpShape = new Shape(null, null);
			
			byteoffset = tmpShape.loadFromFile(dataBuffer, byteoffset);
			
			
			globalThis.importedShapes.push(tmpShape);
			
			
			$('#ligneImportedShapesList').append("<td> </td>");
			
			$('#ligneImportedShapesList td:last').attr('id', globalThis.importedShapes.length - 1);
			
			$('#ligneImportedShapesList td:last').attr('draggable', 'true');
			
			//$('#ligneImportedShapesList td:last').click(function(e){console.log(e.currentTarget.id);});

			
			$('#ligneImportedShapesList td:last').on({
						dragStart: function(e){
						console.log("Starting drag");
						},
						dragenter: function(e){
						console.log("Entering drag");
						},
						dragleave: function(e){
						console.log("Leaving drag");
						},
						dragover: function(e){
						console.log("over drag");
						},
						drop: function(e){
						console.log("drop drag");
						},
						dragend: function(e){
						console.log(e);
						console.log(" x -> " + e.clientX + " y -> " + e.clientY);
						console.log("end drag");
						},
						click: function(e){
							//alert(e.currentTarget.id);
							Application.setFunction(false,false,false,e.currentTarget.id);
						}
			});
			
			// Rescaling the tmpShape if it's too big
			// Maybe ...?
			
			// Adding it to the table
			$('#ligneImportedShapesList td:last').append(tmpShape.canvas.getCanvas());
		};
		
		
		
	} // End of import shape
	
	this.exportDrawing = function(data){
	
		// Setting up the beginning of the file !
		
		// File size used for allocating a correct dataView size
		var fileSize = 0;
		
		// Current byte reading
		var byteoffset = 0;
		
		fileSize = fileSize + 2; // 2 bytes
		
		for (var i = 0 ; i < data.length ; i++){
			fileSize = fileSize + data[i].getBinarySize();
		}
		
		var buffer = new ArrayBuffer(fileSize); 
		
		var dataView = new DataView(buffer);
		
		// Filling the dataView
		
		dataView.setUint16(byteoffset, data.length); byteoffset += 2;
		
		for(var i = 0 ; i < data.length ; i++){
			byteoffset = data[i].generateHexString(dataView, byteoffset);
		}
		
		// Creating the blob
		
		var saveBlob = new Blob([dataView], {type: 'application/octet-binary'});
		
		// Saving
		saveAs(saveBlob, document.getElementById("saveFileName").value + FileHandler.drawingExtension);
		
	}
	
	this.importDrawing = function(){
	}
}